﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using Clothes.Items.ModuleData;
using ApplicationLibrary.DBManager;
using ApplicationLibrary;

namespace Clothes.Items.Scrounger
{
    public partial class TabItemsMaster : UserControl, iScroungerUserControl
    {
        private const string ItemsSlave = "ITEMSSLAVE";
        private EnumsManager<ItemStatus> eItemStatusManager;
        private EnumsManager<InvoiceStatus> eInvoiceStatusManager;

        public TabItemsMaster()
        {
            InitializeComponent();
        }

        public void OnAttachData(ApplicationLibrary.DBManager.DBManager dbManager)
        {
            dgwItems.AutoGenerateColumns = false;
            dgwItems.DataView.Table = dbManager.Dataset.Tables[ItemsSlave];

            dgwItems.AddReadOnlyColumn(CL_Items.ID, true);
            dgwItems.AddReadOnlyColumn(CL_Items.Description, true);
            dgwItems.AddReadOnlyColumn(CL_Items.Price, true);
            dgwItems.AddReadOnlyColumn(CL_Items.Status, true);
            dgwItems.AddReadOnlyColumn(CL_Items.PriceSale, true);
            dgwItems.AddReadOnlyColumn(CL_Items.ReturnToCustomerNo, true);
            dgwItems.AddReadOnlyColumn(CL_Items.InvoiceNo, true);
            dgwItems.AddReadOnlyColumn(CL_Items.BillOfLadingNo, true);

            itc_Status.EnumsType = typeof(ItemStatus);
            itc_Status.ResourceManager = Properties.Resources.ResourceManager;

            eItemStatusManager = new EnumsManager<ItemStatus>(tscbStatus, Properties.Resources.ResourceManager);
            eInvoiceStatusManager = new EnumsManager<InvoiceStatus>(tscbInvoice, Properties.Resources.ResourceManager);
        }

        public void OnBindData(ApplicationLibrary.DBManager.iScrounger frm)
        {
            DocumentForm doc = (DocumentForm)frm;
            doc.BindColumn(itc_Item, CL_Items.ID);
            doc.BindColumn(itc_Descr, CL_Items.Description);
            doc.BindColumn(itc_Price, CL_Items.Price);
            doc.BindColumn(itc_Status, CL_Items.Status);
            doc.BindColumn(itc_PrzSale, CL_Items.PriceSale);
            doc.BindColumn(itc_Invoice, CL_Items.InvoiceNo);
            doc.BindColumn(itc_BillOfLading, CL_Items.BillOfLadingNo);
            doc.BindColumn(itc_ReturnToCust, CL_Items.ReturnToCustomerNo);
            doc.BindControl(dgwItems);

            tscbStatus.SelectedIndex = ItemStatus.E_All.Int();
            tscbInvoice.SelectedIndex = InvoiceStatus.E_All.Int();
        }

        public void OnPrepareAuxData()
        {
        }

        private void dgwItems_DataGridViewDoubleClickCell(object sender, DataGridViewDoubleClickArgs e)
        {
            if (e.ColumnName == CL_Items.ID.Name)
            {
                Items.Forms.Items frmItems = GlobalInfo.OpenForm("Clothes.Clothes.Items.Forms.Items", false) as Items.Forms.Items;
                frmItems.FindRecord(new RadarItemsParam(int.Parse(e.CellValue)));
            }

            if (e.ColumnName == CL_Items.Description.Name)
            {
                Items.Forms.Items frmItems = GlobalInfo.OpenForm("Clothes.Clothes.Items.Forms.Items", false) as Items.Forms.Items;
                frmItems.FindRecord(new RadarItemsParam( dgwItems.GetValue<int>(e.Row, CL_Items.ID)));
            }
        }

        private void tscbStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            CreateFilter();
        }

        private void tscbInvoice_SelectedIndexChanged(object sender, EventArgs e)
        {
            CreateFilter();
        }

        private void CreateFilter()
        {
            string filter = string.Empty;
            switch(tscbInvoice.SelectedIndex)
            {
                case 0:
                    filter = filter.SeparConcat(string.Format("{0} = '' AND {1} ='' AND {2} =''",CL_Items.InvoiceNo.Name, CL_Items.BillOfLadingNo.Name, CL_Items.ReturnToCustomerNo.Name  )," AND ");
                    break;
                case 1:
                    filter = filter.SeparConcat(string.Format("{0} <> ''", CL_Items.InvoiceNo.Name), " AND ");
                    break;
            }

            if (tscbStatus.SelectedIndex <5)
                filter = filter.SeparConcat(string.Format("{0}={1}", CL_Items.Status.Name, tscbStatus.SelectedIndex), " AND ");

            try
            {
                dgwItems.DataView.RowFilter = filter;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
        }
    }
}
